import React, {PropTypes, Component} from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import assign from 'object-assign';
import lyby from '../../util';

import "./index.scss";

export default class MainCmp extends Component{
    _clickPageNum(num){
        if(num == 'prev'){
            num = this.props.page - 1;
        }else if(num == 'next'){
            num = this.props.page + 1;
        }
        this.props.clickPageNum(num);
    }
    render  () {
        let curPage = this.props.page;
        let lastPage;
        if (!this.props.totalPages) {
            lastPage = Math.ceil(this.props.totalCount / this.props.size);
        } else {
            lastPage = this.props.totalPages;
        }
        if(!lastPage){
            return null;
        }

        if(lyby.ismobile){
            return(
                <div className="page_load mobile">
                    <span onClick={this._clickPageNum.bind(this,"prev")} className={classNames({"pageNum":true,"left":true,"disabled":curPage == 1})}>上一页</span>
                    <div className="pageText">{curPage}/{lastPage}</div>
                    <span onClick={this._clickPageNum.bind(this,"next")} className={classNames({"pageNum":true,"right":true,"disabled":curPage == lastPage})}>下一页</span>
                </div>
            );
        }

        let pagesList=[{num:'prev'},{num:1}];
        if(curPage-3>1){
            pagesList.push({num:0});
        }
        for(let i = curPage-2;i<= curPage+2;i++){
            if(i>1 && i<lastPage){
                pagesList.push({num:i});
            }
        }
        if(curPage+3 < lastPage){
            pagesList.push({num:0});
        }
        if(lastPage == 1){
            pagesList.push({num:'next'});
        }else{
            pagesList.push({num:lastPage},{num:'next'});
        }

        return(
            <div className="page_load">
                {
                    pagesList.map((item,index) =>{
                        return(
                            <span key={index} onClick={this._clickPageNum.bind(this,item.num)}  className={classNames({'pageNum':true,'current':item.num == curPage,'disabled':((curPage == 1 && item.num =='prev') || (curPage == lastPage && item.num =='next') || item.num == 0)})}>
                               {item.num == 'prev'?'<<':(item.num == 'next'?'>>':(item.num == 0?'…':item.num))}
                           </span>
                        );
                    })
                }
            </div>
        );

    }
}


MainCmp.propTypes = {
    page : PropTypes.number,
    totalCount : PropTypes.number,
    size : PropTypes.number,
    totalPages : PropTypes.number,
    clickPageNum : PropTypes.func
};